1659B - Bit Flipping - CodeForces Solution


constructive algorithms greedy strings

Please click on ads to support us..

Python Code:

import math
import random
import sys




p2D = lambda x: print(*x, sep="\n")


def II(): return int(sys.stdin.buffer.readline())


def MI(): return map(int, sys.stdin.buffer.readline().split())


def LI(): return list(map(int, sys.stdin.buffer.readline().split()))


def LLI(rows_number): return [LI() for _ in range(rows_number)]


def BI(): return sys.stdin.buffer.readline().rstrip()


def SI(): return sys.stdin.buffer.readline().rstrip().decode()


def li(): return [int(i) for i in input().split()]


def lli(rows): return [li() for _ in range(rows)]


def si(): return input()


def ii(): return int(input())


def ins(): return input().split()



def drank(d, processing, da, rank):
    tmp = 10 ** 9
    if len(d[da]) == 1:
        return 1
    for di in d[da]:
        if processing[di - 1] == 0:
            processing[di - 1] = 1
            tmp = min(tmp, drank(d, processing, di, rank))
            processing[di - 1] = 0
    rank[da - 1] = tmp + 1
    return tmp + 1


def binary_search(n, a):
    l = len(a)
    low = 0
    high = l - 1
    while high >= low:
        mid = (high + low) // 2
        if a[mid] == n:
            return mid
        elif a[mid] > n:
            high = mid - 1
        else:
            low = mid + 1
    return -1





def gcd(a,b):
    if b==0:
        x = 1
        y = 0
        return x, y, a
    x, y, g = gcd(b, a%b)
    return y, x- (a//b)*y, g

def solve():
    n, k = LI()
    s = SI()
    cnts = [0] * n
    p = k%2
    for i in range(n):
        if s[i] == str(p) and k:
            cnts[i] += 1
            k-=1
    cnts[-1] += k
    return ''.join([str(((cnts[i] + p) % 2) ^ int(s[i])) for i in range(n)])+ '\n'+' '.join(list(map(str, cnts)))


def main():
            for _ in range(II()):
        sys.stdout.write(str(solve()) + "\n")
                                    
            



if __name__ == "__main__":
    main()

C++ Code:

#include <iostream>
#include <map>
#include<unordered_map>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include<utility>
#include<climits>
#include<stack>

#define ull unsigned long long
#define ll long long
//#define NUM 1000000001
//ull mod  = 998244353;
using namespace std;


void solve()
{
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    vector<int> ans(n,0);
    int tmp = k;
    for(int i=0;i<n and tmp >0 ;i++)
    {
        if(k%2 == s[i] - '0')
        {
            ans[i] = 1;
            tmp--;
        }
    }
    ans[n-1] += tmp;
    for(int i=0;i<n;i++)
    {
        if((k-ans[i])%2 == 1)
        {
            if(s[i] == '1') s[i] = '0';
            else s[i] = '1';
        }
    }
    cout<<s<<endl;
    for(int i=0;i<n;i++)
    {
        cout<<ans[i]<<" ";
    }
    cout<<endl;

}
int main()
{
    int t ;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading
1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters